/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: AdminBrandShowServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/29 1:12
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.bo.AdminBrandSearchBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminBrandShowServiceImpl implements AdminBrandShowService{
    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public CommonData<MarketBrand> getBrandInfoByInput(AdminBrandSearchBo adminBrandSearchBo) {
        //开启分页功能
        PageHelper.startPage(adminBrandSearchBo.getPage(),adminBrandSearchBo.getLimit());

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        List<MarketBrand> marketBrands;
        //下面的查询都应该是查询转改码为0的数据
        criteria.andDeletedLessThan(true);

        //如果id和名字都是null的时候   查询全部
        if(adminBrandSearchBo.getName()==null&&adminBrandSearchBo.getId()==null){
             marketBrands= brandMapper.selectByExample(marketBrandExample);
             //如果id不是null名字是null 按照id精确查询
        }else if(adminBrandSearchBo.getId()!=null&&adminBrandSearchBo.getName()==null){
            //根据id精确查找
            criteria.andIdEqualTo(adminBrandSearchBo.getId());
            marketBrands = brandMapper.selectByExample(marketBrandExample);
            //到这里id是null  名字不是null的时候
            }else if(adminBrandSearchBo.getId()==null&&adminBrandSearchBo.getName()!=null) {
            //当id和名字都不是null的时候 是进行模糊查询
            criteria.andNameLike("%"+adminBrandSearchBo.getName()+"%");
            marketBrands = brandMapper.selectByExample(marketBrandExample);
            //两个都不是null情况下
            }else{
            //精确加模糊
            criteria.andNameLike("%"+adminBrandSearchBo.getName()+"%");
            criteria.andIdEqualTo(adminBrandSearchBo.getId());
            marketBrands= brandMapper.selectByExample(marketBrandExample);
        }

        //设定查询条件

        //将查询结构封装到返回体的对象中
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        CommonData data = CommonData.data(marketBrandPageInfo);
        return data;
    }
/*
* 删除品牌商
* */
    @Override
    public void deleteBrandByClick(MarketBrand marketBrand) {
        MarketBrand marketBrand2Del = new MarketBrand();
        //获得想要操作的对象
        marketBrand2Del.setId(marketBrand.getId());
        //marketBrand2Del.setDeleted(!marketBrand.getDeleted());
        marketBrand2Del.setDeleted(true);
        int infectRows = brandMapper.updateByPrimaryKeySelective(marketBrand2Del);
        System.out.println(infectRows);
    }
/*
* 更改品牌信息
* */
    @Override
    public MarketBrand updateBrandInfo(MarketBrand marketBrand) {
       // MarketBrand marketBrand2update = new MarketBrand();
        //创建目标Example对象
        MarketBrandExample delBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = delBrandExample.createCriteria();
        criteria.andIdEqualTo(marketBrand.getId());
        //根据传入的对象直接更新原数据
        int infectRows = brandMapper.updateByPrimaryKeySelective(marketBrand);
        System.out.println(infectRows);
        return marketBrand;
    }
/*
* 添加品牌商信息到数据库
* */
    @Override
    public MarketBrand creatBrand(MarketBrand marketBrand) {
        //获得添加文件时间
        long currentTimeMillis = System.currentTimeMillis();
        Date createDate = new Date(currentTimeMillis);
        //首次添加 更新时间和添加时间一致
        marketBrand.setAddTime(createDate);
        marketBrand.setUpdateTime(createDate);
        //将添加的对象加入到数据库中
        int infectRows = brandMapper.insertSelective(marketBrand);
        System.out.println(infectRows);
        return marketBrand;
    }
}
